<?

$xajaxUrl = $baseURL.'/'.$webDIR.'/?sid='.$sid.'&action=manageDomains';
$sections = array(
	'load' => '',
	'error' => '',
	'notice' => '',
	'page' => '',
	'errorMsg'=>'',
	'okMsg'=>''
);
$allowedHtmlObjects = array('manageDomains','registerDomains','myDomains','existingDomains','managednsDomains');
// PRIVATE FUNCTIONS

function LoadPage ($objResponse, $destinationObject, $htmlObject, $type='htmlPage') {
	switch ($type){
		case 'htmlPage':
			$filename = 'domain/'.$htmlObject.'.html';
			if(!is_file($filename) && in_array($htmlObject, $GLOBALS['allowedHtmlObjects'])){
				return false;
			}

			ob_start();
			include_once($filename);
			$data = ob_get_contents();
			ob_end_clean();
		break;
		case 'URL':
			ob_start();
			include_once($htmlObject);
			$data = ob_get_contents();
			ob_end_clean();
			//mail('nd4wd@bastun.net','debug','nd4wd :'.$data);
		break;
		case 'htmlSource':
			$data = $htmlObject;
		break;
	}

	$objResponse->addAssign($destinationObject, 'innerHTML', $data);
}
/****************************************************************************************/

function Show ($objResponse, $div, $style='visibility') {
	if($style == 'visibility'){
		$set_style = 'style.visibility';
		$status = 'visible';
	}elseif($style == 'display'){
		$set_style = 'style.display';
		$status = 'block';
	}
	$objResponse->addAssign($div, $set_style, $status);
}
/****************************************************************************************/

function Hide ($objResponse, $div, $style='visibility') {	
	if($style == 'visibility'){
		$set_style = 'style.visibility';
		$status = 'hidden';
	}elseif($style == 'display'){
		$set_style = 'style.display';
		$status = 'none';
	}
	$objResponse->addAssign($div, $set_style, $status);
}
/****************************************************************************************/

function errorMsg ($objResponse, $msg) {
	$objResponse->addAssign('errorMsg','innerHTML',$msg);
	Hide(&$objResponse, 'notice', 'display');
	Hide(&$objResponse, 'load');
	Show(&$objResponse, 'error', 'display');
}
/****************************************************************************************/

function okMsg ($objResponse, $msg) {
	$objResponse->addAssign('okMsg','innerHTML',$msg);
	Hide(&$objResponse, 'error', 'display');
	Hide(&$objResponse, 'load');
	Show(&$objResponse, 'notice', 'display');
}
/****************************************************************************************/

function debugObject($objResponse, $obj, $divId){
	ob_start();
	print_r($obj);
	$data = ob_get_contents();
	ob_end_clean();
	$objResponse->addAssign('debug', 'innerHTML', $data);
	$objResponse->addScript("var timeoutID = setTimeout(\"document.getElementById('$divId').innerHTML='';\",15000);");
}
/****************************************************************************************/

function Run($action, $params){
	//$params = getParams($params);
	return $action($params);
}
/****************************************************************************************/

function makeGlobals($params){
	foreach($params as $k=>$v){
		$GLOBALS[$k] = $v;
	}
}
/****************************************************************************************/

function getParams($params) {
	if($params){
		$tmp1 = explode('|', $params);
		foreach($tmp1 as $k => $v){
			$tmp2 = explode('::', $v);
			if(substr($tmp2[1],strlen($tmp2[1])-1) == '\\'){
				$tmp2[1] = substr($tmp2[1],0,strlen($tmp2[1])-1);
			}
			eval('$'.$tmp2[0]." = '".str_replace("'", "\'", $tmp2[1])."';");
		}
	}
	unset ($tmp1, $tmp2, $k, $v, $params);
	return get_defined_vars();
}
/****************************************************************************************/

function myLoadPage2($objResponse, $page, $params, $type='htmlPage'){
	$params_array = getParams($params);
	if(!empty($params_array)){
		makeGlobals($params_array);
	}
	LoadPage(&$objResponse, 'page', $page, $type);	
	Hide(&$objResponse, 'load');
	Hide(&$objResponse, 'error','display');
	Hide(&$objResponse, 'notice','display');
}
/****************************************************************************************/
// PUBLIC FUNCTIONS

function myLoadPage($page, $params, $set_params = ''){
	$params_array = getParams($params);
	if(!empty($params_array)){
		makeGlobals($params_array);
	}

        if($set_params){
        	$GLOBALS['set_params'] = $set_params;
	}
                                
	$objResponse = new xajaxResponse();
	LoadPage(&$objResponse, 'page', $page);	
	Hide(&$objResponse, 'load');
	Hide(&$objResponse, 'error','display');
	Hide(&$objResponse, 'notice','display');
	return $objResponse->getXML();
}
/****************************************************************************************/

function addDomain ($id,$domain,$pass,$repass){
	$objResponse = new xajaxResponse();
	if(domain_exist($domain)){
		$errorMsg = translate('domain_exist');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}elseif(!preg_match("/^([a-z0-9][a-z0-9-]?)*[a-z0-9]\.([a-z]{2,4})(\.[a-z]{2,4})?$/",$domain)){
		$errorMsg = translate('invalid_domain_name');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();	
	}elseif(!$pass){
		$errorMsg = translate('invalid_password_any');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}elseif($pass != $repass){
		$errorMsg = translate('invalid_repassword');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}elseif(strlen($pass)>16 or strlen($pass)<5){
		$errorMsg = translate('invalid_passlen');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}
	elseif(preg_match("/\.{1}/",$domain) and preg_match("/\.com\$|\.net\$|\.org\$|\.biz\$|\.info\$|\.us\$/",$domain)){
		$domain_search_result = search4domains($domain);
		if($domain_search_result[$domain] == 'available'){
			$errorMsg = translate('domain_not_registered');
			errorMsg(&$objResponse, $errorMsg);
			return $objResponse->getXML();
		}
	}

	if(!add_domain_rp2($id,$domain)){
		$errorMsg = translate('error_adding_domain');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	if(!add_domain_mailbox_email($id,$domain,$pass)){
		$errorMsg = translate('error_creating_domain_mailbox');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	$okMsg = translate('successfully_added_domain');
	okMsg(&$objResponse, $okMsg);
	$objResponse->addScript("LoadPage('myDomains');");
	return $objResponse->getXML();
}
/****************************************************************************************/

function searchDomain($params){
	$params_array = getParams($params);
	$domain = $params_array['search_domain'];
	if($domain == 'example.com' and $params_array['domain']){
		$domain = substr(strstr($params_array['domain'],'_'),1);
	}

	if(preg_match("/^www\./", $domain)){
		$domain = substr($domain, 4);
	}

	$result = search4domains($domain);
	$objResponse = new xajaxResponse();
	if($result === false){
		$errorMsg = translate('invalid_domain_name');
		errorMsg(&$objResponse, $errorMsg);
		$objResponse->addAssign('search_domain', 'value', 'example.com');
		return $objResponse->getXML();
	}

	$map = array('notavailable'=>'Transfer','available'=>'Register');
	$tbl = '';
	foreach($result as $k=>$v){
		  $tbl .= "<tr onClick=\"document.getElementById('dom_$k').click();\">
				  <td id='td_$k' nowrap>
						  <input type='radio' id='dom_$k' name='domain' value='".$v."_".$k."' onClick=\"bold_this('td_$k');show_domain_tooltip(this.value);\">".($v == 'notavailable' ? '<font color="#DA800E">'.$map[$v].'</font>' : '<font color="#006600">'.$map[$v].'</font>')." $k
				  </td>
		  </tr>";
	}

	$gl['domain_search_result'] = "<table cellspacing=0 cellpadding=0 class=domainTable width='100%'>
			<tr>
				<td class=Bold nowrap>Register/Transfer Domain:</td>
			</tr>
			$tbl
			<tr><td><input type=hidden name='params' id='params' value='$params'></td></tr>
		</table>";
	makeGlobals($gl);
	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step1');
	$objResponse->addScript("if(document.getElementById('dom_$domain')){document.getElementById('dom_$domain').click();}else if(document.getElementById('dom_".$domain.".com')){document.getElementById('dom_".$domain.".com').click();}");
	$objResponse->addAssign('params', 'value', $params);
	return $objResponse->getXML();
}
/****************************************************************************************/

function chooseDomain($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	if(!$params_array['domain']){
		$errorMsg = 'No Domain Selected';
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	$p['domain_name'] = substr(strstr($params_array['domain'],'_'),1);
	$p['domain_action'] = substr($params_array['domain'], 0, strpos($params_array['domain'], '_'));
	$p['products'] = $params_array['products'] ? $params_array['products'] : 'p1';
	$p['ns_type'] = $params_array['ns_type'] ? $params_array['ns_type'] : 'ns1';
	
	$p['dns_features'] = $params_array['dns_features'] ? $params_array['dns_features'] : 1;

	$domain_list = list_reseller_domains_($GLOBALS['id']);
	if($domain_list){
		foreach($domain_list as $dom=>$arr){
			if($arr['dns_purchase_id']){
				$p['products'] = 'p3';
				break;
			}
		}
	}

	if($p['domain_action'] == 'notavailable'){
		$p['products'] = 'p3';
	}

	makeGlobals($p);

	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step2');
	$objResponse->addScript("focused = {'products':'tbl_p1','ns_type':'tbl_ns1'};document.getElementById('products".preg_replace("/p/","",$p['products'])."').click();document.getElementById('".preg_replace("/ns/","ns_type",$p['ns_type'])."').click();document.getElementById('dns_features".$p['dns_features']."').click();");
	$objResponse->addAssign('params', 'value', $params);
	return $objResponse->getXML(); 
}
/****************************************************************************************/

function choosePlan($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	
	if(!$params_array['products'] or !$params_array['ns_type']){
                $errorMsg = 'No Purpose Selected';
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();
        }
			

	$p['domain_name'] = substr(strstr($params_array['domain'],'_'),1);
	$p['pass'] = $params_array['pass'];
	$p['repass'] = $params_array['repass'];
	makeGlobals($p);
	
	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step3');
	$objResponse->addAssign('params', 'value', $params);
	return $objResponse->getXML(); 
}
/****************************************************************************************/

function mailBox($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	if(!$params_array['pass']){
		$errorMsg = translate('invalid_password_any');
		errorMsg(&$objResponse, $errorMsg);
		$objResponse->addAssign('pass','value','');
		$objResponse->addAssign('repass','value','');
		return $objResponse->getXML();
	}elseif($params_array['pass'] != $params_array['repass']){
		$errorMsg = translate('invalid_repassword');
		errorMsg(&$objResponse, $errorMsg);
		$objResponse->addAssign('pass','value','');
		$objResponse->addAssign('repass','value','');
		return $objResponse->getXML();
	}elseif(strlen($params_array['pass'])>16 or strlen($params_array['pass'])<5){
		$errorMsg = translate('invalid_passlen');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}
	
	if(!$params_array['info']){
//		$params_array['info'] = getResellerInfo($GLOBALS['id']);
		$params_array['info'] = utf8EscapeArray(getResellerInfo($GLOBALS['id']));
	}
	$p['domain_name'] = substr(strstr($params_array['domain'],'_'),1);;
	$p['info'] = $params_array['info'];
	makeGlobals($p);
	
	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step4');
	$objResponse->addAssign('params', 'value', $params);
	
	return $objResponse->getXML();
}
/****************************************************************************************/

function choosePM($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	include_once('domain/valid_info.php');
	$valid = $user_validator->validate_array($params_array['info'], false);
	if(!$valid){
		$err_str = array_flip($user_validator->validate_returned);
		$error_msg_arr = array(
			'first_name'=>'First Name',
			'last_name'=>'Last Name',
			'company'=>'Organization',
			'job_title'=>'Job Title',
			'email'=>'Email',
			'address1'=>'Address1',
			'address2'=>'Address2',
			'city'=>'City',
			'state'=>'State',
			'zip'=>'Zip',
			'country'=>'Country',
			'phone'=>'Phone',
			'fax'=>'Fax',
			'vat_num' => 'Vat Number'
		);
		$errorMsg = 'Invalid '.$error_msg_arr[($err_str[0])];
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();	
	}
	
        $p['domain_name'] = substr(strstr($params_array['domain'],'_'),1);
        $p['dom_action'] = substr($params_array['domain'], 0, strpos($params_array['domain'], '_'));
	$p['mail_server'] =  $params_array['mail_server'];
	$p['products'] =  $params_array['products'];
	$p['ns_type'] = $params_array['ns_type'];
	$p['payment_method'] = $params_array['payment_method'] ? $params_array['payment_method'] : 'cc';
	

        makeGlobals($p);

        //debugObject(&$objResponse, $p, 'debug');
	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step5');
	$objResponse->addAssign('params', 'value', $params);
	return $objResponse->getXML(); 
}
/****************************************************************************************/

function submitPayment($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	
	if($params_array['terms']!='yes'){
                $errorMsg = 'You must agree with our terms and conditions!';
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();		
	}elseif(!$params_array['payment_method']){
                $errorMsg = 'Please select a payment method!';
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();	
	}

	$domain = substr($params_array['domain'], strpos($params_array['domain'], '_') + 1);
	$act = substr($params_array['domain'], 0, strpos($params_array['domain'], '_'));
	$form = '<form name="submitPayment" id="submitPayment" method="post" action="'.$GLOBALS['remoteIncludeURL'].'upgrade/res_regdomain1.php">';

	foreach($GLOBALS['dns_products']->product as $k => $v){
		if($v->type == 'res_regdomain'){
			$domain_rp_product_id = $k;
		}
	}

	$ordered_rp_product_id = $GLOBALS['rp_products'][($params_array['products'])];
	$ordered = $GLOBALS['dns_products']->product[$ordered_rp_product_id];

	if($ordered->type == 'res_regdomain'){
		switch ($act){
			case 'renewal':
				$action = 'renewal';
				$domain_action = 'renewal';
			break;
			case 'available':
				$action = 'order';
				$domain_action = 'register';
			break;
			case 'notavailable':
				$action = 'order';
				$domain_action = 'transfer';
			break;
		}
	}elseif($ordered->type == 'dns_package'){
		switch ($act){
			case 'addmdns1':
				$action = 'order';
				$domain_action = 'renewal';
			break;
			case 'addmdns2':
				$action = 'order';
				$domain_action = 'none';
			break;
			case 'renewal':
				$action = 'renewal';
				$domain_action = 'renewal';
			break;
			case 'available':
				$action = 'order';
				$domain_action = 'register';
			break;
			case 'notavailable':
				$action = 'order';
				$domain_action = 'transfer';
			break;
		}

		$mail_server = $params_array['mail_server'] ? 't' : 'f';
		$open_store = $params_array['open_store'] ? 't' : 'f';
		$enabled = $params_array['enabled'] ? 't' : 'f';
		$ns_type = $GLOBALS['ns_types'][($params_array['ns_type'])];

		$form .= "<input type='hidden' name='dns_cluster[rp_product_id]' value='$ordered_rp_product_id'>";
		$form .= "<input type='hidden' name='dns_cluster[mail_server]' value='$mail_server'>";
		$form .= "<input type='hidden' name='dns_cluster[open_store]' value='$open_store'>";
		$form .= "<input type='hidden' name='dns_cluster[enabled]' value='$enabled'>";		
		$form .= "<input type='hidden' name='dns_cluster[ns_type]' value='$ns_type'>";
	}

	if(in_array($act, array('addmdns1', 'addmdns2', 'renewal'))){
		$params_array['info'] = getResellerInfo($GLOBALS['id']);
	}

	$form .= "<input type='hidden' name='control_action' value='".$ordered->type."'>";
	$form .= "<input type='hidden' name='action' value='$action'>";
	$form .= "<input type='hidden' name='processor' value='$params_array[payment_method]'>";

	$form .= "<input type='hidden' name='domain[name]' value='$domain'>";
	$form .= "<input type='hidden' name='domain[action]' value='$domain_action'>";
	$form .= "<input type='hidden' name='domain[rp_product_id]' value='$domain_rp_product_id'>";

	foreach($params_array['info'] as $k=>$v){
		$form .= "<input type='hidden' name='reseller[$k]' value='$v'>";
	}
	$form .= "<input type='hidden' name='reseller[reseller_id]' value='$GLOBALS[id]'>";

	$url_params['sid'] = $GLOBALS['sid'];
	$url_params['action'] = 'manageDomains';

	$_SESSION['order']['action'] = $action;
	$_SESSION['order']['domain_action'] = $domain_action;
	$_SESSION['order']['domain'] = $domain;
	$_SESSION['order']['pass'] = $params_array['pass'];
	$_SESSION['order']['type'] = $ordered->type;
	
	$form .= "<input type='hidden' name='return_url' value='".$GLOBALS['baseURL'].'/'.$GLOBALS['webDIR'].'/?'.encode_params($url_params)."'>";
	$form .= "</form>";

	$objResponse->addAssign('submitForm', 'innerHTML', $form);
	$objResponse->addScript("var timeoutID = setTimeout(\"document.getElementById('submitPayment').submit();\",3000);");
	//debugObject(&$objResponse, htmlspecialchars($form), 'debug');
	return $objResponse->getXML(); 
}
/****************************************************************************************/

function delDomains($params){
	$params_array = getParams($params);
	$domains = $params_array['domains'];
        $objResponse = new xajaxResponse();
        //debugObject(&$objResponse, $params_array, 'debug');
	//return $objResponse->getXML();	
        
	$dom = list_reseller_domains_($GLOBALS['id']);
	if(empty($domains)) {
		$errorMsg = translate('no_domains_selected_delete');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	if(is_array($dom) and !in_array($domains, array_keys($dom)))  {
		$errorMsg = translate('int_error');
                errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	if(!delete_domain_rp2($GLOBALS['id'], $domains)){
		$errorMsg = translate('error_delete_domain');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	if(!delete_domain_mailbox_email($GLOBALS['id'], $domains)) {
		$errorMsg = translate('error_delete_domain_mailbox');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();	
	}

	$okMsg = translate('successfully_deleted_domain');
	okMsg(&$objResponse, $okMsg);
	$objResponse->addScript("LoadPage('myDomains');");
	return $objResponse->getXML();
}

/****************************************************************************************/
function renewalServices($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	$params_array['domain'];
	$info = getResellerInfo($GLOBALS['id']);	
	foreach($info as $k => $v){
		$params = $params.'|reseller['.$k.']::'.$v;
	}

	if($params_array['dns_features'] == 1){
		$params = $params.'|mail_server::yes';
		$params = $params.'|open_store::yes';
		$params = $params.'|enabled::yes';
	}elseif($params_array['dns_features'] == 2){
                $params = $params.'|mail_server::yes';
                $params = $params.'|enabled::yes';		
	}elseif($params_array['dns_features'] == 3){
                $params = $params.'|enabled::yes';	
	}
	
	$params_array = getParams($params);
        $p['domain_name'] = substr(strstr($params_array['domain'],'_'),1);
        $p['dom_action'] = substr($params_array['domain'], 0, strpos($params_array['domain'], '_'));
        $p['products'] =  $params_array['products'];
        $p['ns_types'] = $GLOBALS['ns_types'];
        $p['ns_type'] = $params_array['ns_type'];
        $p['mail_server'] = $params_array['mail_server'];
	makeGlobals($p);

	myLoadPage2(&$objResponse, 'registerDomains', 'stepPage::Step5');
	$objResponse->addAssign('params', 'value', $params);
	return $objResponse->getXML();
}
/****************************************************************************************/

function LoadDnsDomainInfo($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	$local_connect = $GLOBALS['master_db'];
	$tmp_res = array();
	$tmp = array();

	//myLoadPage2(&$objResponse, 'managed', '');
	
	if(! $tmp_res = dns_dom_settings($params_array['dns_dom_id'])){
		$errorMsg = translate('error_getting_dns_domain');
		errorMsg(&$objResponse, $errorMsg);
		return $objResponse->getXML();
	}

	$dns_rp_product = getRpProductByPurchaseId($tmp_res['dns_purchase_id']);
	$tmp = array_flip($GLOBALS['rp_products']);
	$dns_prod = $GLOBALS['dns_products']->product[($dns_rp_product['rp_product_id'])];
	$dns_price = sprintf("%01.2f", $dns_prod->getPriceByType());
	if($dns_rp_product['services'] == 'not_shared_dns'){
		$num_ips = 2;
	}elseif($dns_rp_product['services'] == 'shared_dns'){
		$num_ips = 1;
	}

	$url_params['sid'] = $GLOBALS['sid'];
	$url_params['action'] = 'dnsActivationLog';
	$url_params['dns_dom_id'] = $params_array['dns_dom_id'];
	
	$activation_log_url = "$GLOBALS[baseURL]/$GLOBALS[webDIR]/?".encode_params($url_params);
                                

	if($tmp_res['enabled'] == 't'){
		$status = "<font color=green><b>Enabled</b></font><br>( <a href='#' onClick=\"window.open('$activation_log_url','','height=240,width=320,scrollbars=yes');return false;\">View Log</a> )";
		$dns_dom_action = "<input type='submit' value='Disable DNS' onClick=\"if(confirmSubmit('You are about to disable Private DNS service for $tmp_res[dns_dom_name]')){change_store_settings('take_action::disable|dns_dom_id::$params_array[dns_dom_id]');}\">";
	}elseif($tmp_res['enabled'] == 'f'){
		$status = "<font color=red><b>Disabled</b></font><br>( <a href='#' onClick=\"window.open('$activation_log_url','','height=240,width=320,scrollbars=yes');return false;\">View Log</a> )";
		$dns_dom_action = "<input type='submit' value='Enable DNS' onClick=\"if(confirmSubmit('You are about to enable Private DNS service for $tmp_res[dns_dom_name]')){change_store_settings('take_action::enable|dns_dom_id::$params_array[dns_dom_id]');}\">";
	}
	
	$records = get_domain_records($params_array['dns_dom_id'], array('r.type'=>'NS'));
	//debugObject(&$objResponse, $records, 'debug');
	foreach($records as $k=>$v){
		$ns_records[$k] = get_domain_records($params_array['dns_dom_id'], array('r.name'=>$v['content']));
		//debugObject(&$objResponse, $ns_records[$k], 'debug');
		$dnses[] = $v['content'];
		$ips[] = $ns_records[$k][0]['content'];
		$ip_types[] = $ns_records[$k][0]['shared'] == 't' ? 'Shared' : 'Dedicated';
	}
	
	if($tmp_res['mail_server'] == 't'){
		$mail_server = 'mail.'.$tmp_res['dns_dom_name'];
	}elseif($tmp_res['mail_server'] == 'f'){
		$mail_server = "<img align='absmiddle' src='domain/images/ok_no_02.gif' alt='NO' width='11' height='17' border='0'> <a href='#' onClick=\"if(confirmSubmit('You are about to enable mail server mail.$tmp_res[dns_dom_name]')){change_store_settings('take_action::set_mail_server|dns_dom_id::$params_array[dns_dom_id]');}\">[set mail server]</a>";
	}
	
	$table = "<table class=bTable cellpadding=2 cellspacing=0 width=100%><tr>
		<th>Status</th>
		<th>DNS</th>
		<th>IPs</th>
		<th>IP Type</th>
		<th>Mail Server</th>
	</tr><tr>
		<td align=center>".($status)."</td>
                <td align=center>".implode('<br>', $dnses)."</td>
                <td align=center>".implode('<br>', $ips)."</td>
                <td align=center>".implode('<br>', $ip_types)."</td>
                <td align=center>".$mail_server."</td>
	</tr>";
	// warrning seciton

	if($tmp_res['expire_days'] < 31){
		$objResponse->addAssign('dns_warrning', 'style.display', 'block');
		$objResponse->addAssign('package_name_warring', 'innerHTML', $dns_rp_product['name']);
		$objResponse->addAssign('package_price_warring', 'innerHTML', $dns_price);
		$objResponse->addAssign('num_ips_warring', 'innerHTML', $num_ips);
		$objResponse->addAssign('num_days_warring', 'innerHTML', $tmp_res['expire_days']);
		$objResponse->addAssign('dns_clients', 'innerHTML', $tmp_res['users_count']);
		$objResponse->addAssign('expire_date_warrning', 'innerHTML', date("F d, Y", strtotime($tmp_res['expire_date'])));
		//$objResponse->addAssign('', 'style.display', 'block');
	}else{
		$objResponse->addAssign('dns_warrning', 'style.display', 'none');
	}

// dns domain section
/*
	if($tmp['enabled'] == 't' and $tmp_res['open_store'] == 't'){
		$dom_a_record = get_domain_records($params_array['dns_dom_id'], array('r.type'=>'A','r.name'=>$tmp_res['dns_dom_name']));
		//$objResponse->addAssign('open_store','checked','true');
		//$objResponse->addAssign('store_a_record','enabled','true');
		$objResponse->addAssign('store_a_record','value', $dom_a_record[0]['content']);
		$objResponse->AddScript("document.getElementById('open_store').click();");
	}else{
		$query = "SELECT address FROM dns_cluster.addresses WHERE type='store' LIMIT 1";
		$local_connect->query($query);
		$store_address = $local_connect->getNext();
		$objResponse->addAssign('store_a_record','disabled','true');
		$objResponse->addAssign('store_a_record','value',$store_address['address']);
	}
*/

//	$objResponse->addEvent("activation_log", "onclick", "window.open('$activation_log_url','','height=240,width=320,scrollbars=yes');return false;");

	$objResponse->addAssign('current_dns_dom_id', 'value', $params_array['dns_dom_id']);
	$objResponse->addAssign('dns_domain', 'style.display', 'block');
	$objResponse->addAssign('package_name', 'innerHTML', $dns_rp_product['name']);
	$objResponse->addAssign('domain_name', 'innerHTML', $tmp_res['dns_dom_name']);
	$objResponse->addAssign('package_price', 'innerHTML', $dns_price);
	$objResponse->addAssign('num_ips', 'innerHTML', $num_ips);
	$objResponse->addAssign('dns_dom_action', 'innerHTML', $dns_dom_action);
	$objResponse->addAssign('dns_expires_on', 'innerHTML', 'DNS expires on <b>'.date('m/d/Y', strtotime($dns_rp_product['expire_date'])).'</b>');
	$objResponse->addAssign('renew_button', 'innerHTML', "<input type='button' value='Renew DNS' onClick=\"renewalServices('domain::renewal_".$tmp_res['dns_dom_name']."|products::".$tmp[($dns_rp_product['rp_product_id'])]."');\">");
	$objResponse->addAssign('dns_dom_table', 'innerHTML', $table);

        Hide(&$objResponse, 'load');

	if($params_array['errorMsg']){
		errorMsg(&$objResponse, $params_array['errorMsg']);
	}else{
		Hide(&$objResponse, 'error', 'display');
	}

        if($params_array['okMsg']){
                okMsg(&$objResponse, $params_array['okMsg']);
        }else{
		Hide(&$objResponse, 'notice', 'display');
	}

	return $objResponse->getXML();
}
/****************************************************************************************/

function change_store_settings($params){
        $params_array = getParams($params);
        $objResponse = new xajaxResponse();
        $local_connect = $GLOBALS[master_db];

	//debugObject(&$objResponse, $params_array, 'debug');
	//Hide(&$objResponse, 'load');

        switch($params_array['take_action']){
        	case 'enable':
        		$query ="BEGIN;UPDATE dns_cluster.domain_settings SET enabled='t',checked='f' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.domain_settings SET checked='t' WHERE dns_dom_id=$params_array[dns_dom_id];COMMIT;";
			//debugObject(&$objResponse, $query, 'debug');
			$local_connect->query($query);
			if($local_connect->error){
				$errorMsg = translate('error_enabling_dns');
				//errorMsg(&$objResponse, $errorMsg);
				return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|errorMsg::'.$errorMsg);
				//return $objResponse->getXML();
			}

			include_once(LIBS_DIR.'System/System.class.php');
			include_once($GLOBALS[baseDIR].'login/domain/log_setup.php');
			$log = new systemLog($GLOBALS['master_db'], 'comments_resellers', 'id', $log_setup);
			$info['type'] = 'dns_activation_log';
			$info['table_primary_key'] = $params_array['dns_dom_id'];
			$info['author'] = 'system';
			$info['comment'] = 'Enabled';
			$log->add($info);

			$okMsg = translate('dns_enabled');
        		//okMsg(&$objResponse, $okMsg);
			return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|okMsg::'.$okMsg);
			//return $objResponse->getXML();
        	break;
        	case 'disable':
			$query ="BEGIN;UPDATE dns_cluster.domain_settings SET enabled='f',checked='f' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.domain_settings SET checked='t' WHERE dns_dom_id=$params_array[dns_dom_id];COMMIT;";
			//debugObject(&$objResponse, $query, 'debug');
			$local_connect->query($query);
			if($local_connect->error){
				$errorMsg = translate('error_disabling_dns');
				//errorMsg(&$objResponse, $errorMsg);
				return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|errorMsg::'.$errorMsg);
				//return $objResponse->getXML();
			}

                        include_once(LIBS_DIR.'System/System.class.php');
                        include_once($GLOBALS[baseDIR].'login/domain/log_setup.php');
                        $log = new systemLog($local_connect, 'comments_resellers', 'id', $log_setup);
                        $info['type'] = 'dns_activation_log';
                        $info['table_primary_key'] = $params_array['dns_dom_id'];
                        $info['author'] = 'system';
                        $info['comment'] = 'Disabled';
                        $log->add($info);

			$okMsg = translate('dns_disabled');
			//okMsg(&$objResponse, $okMsg);
			return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|okMsg::'.$okMsg);
			//return $objResponse->getXML();
        	break;
		case 'set_mail_server':
			//debugObject(&$objResponse, 'set_mail_server', 'debug');
                        $query ="BEGIN;UPDATE dns_cluster.domain_settings SET mail_server='f',checked='f' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.domain_settings SET mail_server='t',checked='t' WHERE dns_dom_id=$params_array[dns_dom_id];COMMIT;";
                        //debugObject(&$objResponse, $query, 'debug');
                        $local_connect->query($query);
                        if($local_connect->error){
                                $errorMsg = translate('error_set_mail_server');
                                //errorMsg(&$objResponse, $errorMsg);
				return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|errorMsg::'.$errorMsg);
                                //return $objResponse->getXML();
                        }
                        $okMsg = translate('set_mail_server_ok');
                        //okMsg(&$objResponse, $okMsg);
                        return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id'].'|okMsg::'.$okMsg);
                        //return $objResponse->getXML();
		break;
		case 'change_a_record':
                        //debugObject(&$objResponse, 'set_mail_server', 'debug');
                        //$params_array['dns_dom_id'] = $params_array['current_dns_dom_id'];
			if($params_array['open_store'] == 'yes'){
				if(! $preg_res = preg_match("/^(\d{1,3}\.){3}\d{1,3}$/",$params_array['store_a_record'])){
        	                        $errorMsg = translate('invalid_a_record');
	                                errorMsg(&$objResponse, $errorMsg);
					//return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id']);
                			return $objResponse->getXML();
				}
                                $query ="BEGIN;UPDATE dns_cluster.domain_settings SET open_store='f',checked='f' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.domain_settings SET open_store='t', checked='t' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.records SET content = '$params_array[store_a_record]' WHERE domain_id=$params_array[dns_dom_id] AND type='A' AND name IN((SELECT name FROM dns_cluster.domains WHERE id=$params_array[dns_dom_id]),'www.'||(SELECT name FROM dns_cluster.domains WHERE id=$params_array[dns_dom_id]));COMMIT;";
			}else{
	                        $query ="BEGIN;UPDATE dns_cluster.domain_settings SET open_store='t',checked='f' WHERE dns_dom_id=$params_array[dns_dom_id];UPDATE dns_cluster.domain_settings SET open_store='f', checked='t' WHERE dns_dom_id=$params_array[dns_dom_id];COMMIT;";
			}
                        //debugObject(&$objResponse, $query, 'debug');
                        $local_connect->query($query);
                        if($local_connect->error){
                                $errorMsg = translate('error_change_a_record');
                                errorMsg(&$objResponse, $errorMsg);
				//mail('nd4wd@bastun.net','adsad',$query);
				//$arr = array('88.203.210.112','88.203.210.94');
				//if(in_array($_SERVER[REMOTE_ADDR], $arr)){
					//debugObject($objResponse, $query, 'debug');
				//}
                                //return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id']);
                                return $objResponse->getXML();

                        }
                        $okMsg = translate('change_a_record_ok');
                        okMsg(&$objResponse, $okMsg);
                        //return LoadDnsDomainInfo('dns_dom_id::'.$params_array['dns_dom_id']);
                        return $objResponse->getXML();
		break;
        }

	//return $objResponse->getXML();
}
/****************************************************************************************/

function deleteRecord($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	$local_connect = $GLOBALS['master_db'];
	$query = "DELETE FROM dns_cluster.records WHERE id=$params_array[id]";
	//debugObject(&$objResponse, $query, 'debug');
	//return $objResponse->getXML();
	$local_connect->query($query);
	if($local_connect->error){
		mail('errors@bastun.net', "deleteRecord($params)", $query);
                $errorMsg = translate('error_deleting_record');
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();
	}

	$p['dns_dom_id'] = $params_array['dns_dom_id'];
	makeGlobals($p);

	$okMsg = translate('record_deleted_successfully');
//	okMsg(&$objResponse, $okMsg);
	myLoadPage2(&$objResponse, 'changeRecords', '');
	$objResponse->addScript("var timeoutID = setTimeout(\"document.getElementById('notice').style.display = 'block'; document.getElementById('okMsg').innerHTML='$okMsg';\",1000);");
	return $objResponse->getXML();
}
/****************************************************************************************/

function editRecord($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	$local_connect = $GLOBALS['master_db'];

	if(!preg_match("/^(([a-z0-9][a-z0-9-]?)*[a-z0-9]\.)+([a-z]{2,4})(\.[a-z]{2,4})?$/", $params_array['name']) or strlen($params_array['name']) > 128){
                $errorMsg = translate('invalid_name_record');
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();	
	}

	if($params_array['type'] == 'A'){
		if(!preg_match("/^(\d{1,3}\.){3}\d{1,3}$/", $params_array['content'])){
			$errorMsg = translate('invalid_content_arecord');
			errorMsg(&$objResponse, $errorMsg);
			return $objResponse->getXML();
		}else{
			$query = "UPDATE dns_cluster.records SET name='$params_array[name]', content='$params_array[content]' WHERE id=$params_array[id]";
		}
	}elseif($params_array['type'] == 'MX'){
		if(!preg_match("/^(([a-z0-9][a-z0-9-]?)*[a-z0-9]\.)+([a-z]{2,4})(\.[a-z]{2,4})?$/", $params_array['content']) or strlen($params_array['content']) > 128){
			$errorMsg = translate('invalid_content_mxrecord');
			errorMsg(&$objResponse, $errorMsg);
			return $objResponse->getXML();
		}else{
			$query = "UPDATE dns_cluster.records SET content='$params_array[content]' WHERE id=$params_array[id]";
		}
	}
	
	//debugObject(&$objResponse, $query, 'debug');
	//return $objResponse->getXML();
	$local_connect->query($query);
	if($local_connect->error){
		mail('errors@bastun.net', "editRecord($params)", $query);
                $errorMsg = translate('error_editing_record');
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();
	}
	
        $okMsg = translate('record_edited_successfully');
        okMsg(&$objResponse, $okMsg);
	return $objResponse->getXML();	
}
/****************************************************************************************/

function addRecord($params){
	$params_array = getParams($params);
	$objResponse = new xajaxResponse();
	$local_connect = $GLOBALS['master_db'];
	
	//debugObject(&$objResponse, $params_array, 'debug');

	if(!empty($params_array['record_name'])){
		$params_array['record_name'] = $params_array['record_name'] . '.' . $params_array['dns_dom_name'];
	}else{
		$params_array['record_name'] = $params_array['dns_dom_name'];
	}

	if(!preg_match("/^(([a-z0-9][a-z0-9-]?)*[a-z0-9]\.)+([a-z]{2,4})(\.[a-z]{2,4})?$/", $params_array['record_name']) or !preg_match("/.*".$params_array[dns_dom_name]."$/", $params_array['record_name']) or strlen($params_array['record_name']) > 128){
                $errorMsg = translate('invalid_name_record');
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();	
	}	

	if($params_array['record_type'] == 'A'){
		if(!preg_match("/^(\d{1,3}\.){3}\d{1,3}$/", $params_array['record_content'])){
			$errorMsg = translate('invalid_content_arecord');
			errorMsg(&$objResponse, $errorMsg);
			return $objResponse->getXML();
		}else{
			$query = "INSERT INTO dns_cluster.records (domain_id, name, type, content) VALUES($params_array[record_domain_id], '$params_array[record_name]', '$params_array[record_type]', '$params_array[record_content]')";
		}
	}elseif($params_array['record_type'] == 'MX'){
		if(!preg_match("/^(([a-z0-9][a-z0-9-]?)*[a-z0-9]\.)+([a-z]{2,4})(\.[a-z]{2,4})?$/", $params_array['record_content']) or strlen($params_array['record_content']) > 128){
			$errorMsg = translate('invalid_content_mxrecord');
			errorMsg(&$objResponse, $errorMsg);
			return $objResponse->getXML();
		}else{
			$query = "INSERT INTO dns_cluster.records (domain_id, name, type, content, prio) VALUES($params_array[record_domain_id], '$params_array[record_name]', '$params_array[record_type]', '$params_array[record_content]', 10)";
		}
	}

	//debugObject(&$objResponse, $query, 'debug');
	//return $objResponse->getXML();
	$local_connect->query($query);
	if($local_connect->error){
		mail('errors@bastun.net', "addRecord($params)", $query);
                $errorMsg = translate('error_adding_record');
                errorMsg(&$objResponse, $errorMsg);
                return $objResponse->getXML();
	}
	
	$p['dns_dom_id'] = $params_array['record_domain_id'];
	makeGlobals($p);
        $okMsg = translate('record_added_successfully');
        //okMsg(&$objResponse, $okMsg);
	myLoadPage2(&$objResponse, 'changeRecords', '');
	$objResponse->addScript("var timeoutID = setTimeout(\"document.getElementById('notice').style.display = 'block'; document.getElementById('okMsg').innerHTML='$okMsg';\",1000);");
	return $objResponse->getXML();	
}
/****************************************************************************************/

function utf8EscapeArray($arr = array()) {
	$rs = array();
	
	while(list($key,$val) = each($arr)) {
		if(is_array($val)) {
			$rs[$key] = utf8EscapeArray($val);
		}
		else {
			$rs[$key] = utf8_encode($val);
		}
	}
	return $rs;
}
/****************************************************************************************/

function utf8UnEscapeArray($arr = array()) {
        $rs = array();

        while(list($key,$val) = each($arr)) {
                if(is_array($val)) {
                        $rs[$key] = utf8UnEscapeArray($val);
                }
                else {
			$rs[$key] = utf8_decode($val);
                }
        }
        return $rs;
}


$xajaxRegisterFunctions = array (
	'myLoadPage' => XAJAX_GET,
	'addDomain' => XAJAX_GET,
	'Run' => XAJAX_GET
);

?>